import WxRequest from '../assets/plugins/wx-request/lib/index'

class Base extends WxRequest {
  constructor(options) {
    super(options)
    this.$$prefix = 'http://photo.local/api';
    this.$$prefix = 'https://zxg.shu-re.com/api';
    this.interceptors.use({
      request(request) {
        request.header.token = wx.getStorageSync("token");
        wx.showLoading({ title: '加载中...',})
        if (!request.data) {
          request.data = {}
        }
        var store = getApp().globalData.curStore;
        if (!request.data.hasOwnProperty("store")&&store&&store.id) {
          request.data = Object.assign(request.data, { 'store_id': store.id})
        }
        return request
      },
      requestError(requestError) {
        wx.hideLoading()
        return Promise.reject(requestError)
      },
      response(response) {
        wx.hideLoading()
        if (response.data.code == 50008) {
          getApp().getUserInfo(function (userInfo) {
            //更新数据,获取当前页面，调用onShow进行重新刷新数据
            var pages = getCurrentPages();
            var currentPage = pages[pages.length - 1]
            currentPage.onShow()
          })
          return Promise.reject(response.data);;
        }
        if (response.data.code != 200) {
          wx.showToast({
            title: '请求失败!,错误信息:' + response.data.other,
            icon: 'none',
          })
          return Promise.reject(response.data);
        }
        return response.data
      },
      responseError(responseError) {
        wx.hideLoading()
        return Promise.reject(responseError)
      },
    });
  }
}
export {
  Base
};